<html>
<head>
    <meta charset="utf-8">
    <title>登录系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/module/plugins/login/login.css" media="all">
</head>

<body>
<div class="layadmin-user-login layadmin-user-display-show">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>登录系统</h2>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username" ></label>
                <input autofocus="autofocus" type="text" name="loginAccount" lay-verify="required" placeholder="用户名" class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password"></label>
                <input type="password" name="loginPassword" lay-verify="required" placeholder="密码" class="layui-input">
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"></label>
                        <input type="text" name="verifyCode" lay-verify="required" placeholder="图形验证码" class="layui-input">
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <img src="" class="layadmin-user-login-codeimg">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="margin-bottom: 20px;">
                <a href="forget.html" class="layadmin-user-jump-change layadmin-link" style="margin-top: 7px;">
                    忘记密码？
                </a>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid loginBtn" lay-submit="" lay-filter="login">登 入</button>
            </div>
        </div>
    </div>
</div>
<script src="/plugins/layui/layui.js"></script>
<script src="/plugins/common.js"></script>
<script>
    //登录链接
    var verifyUrl = SERVER_URL+"/sysUser/verifyCode";
    var loginUrl = SERVER_URL+"/sysUser/login";
    var loginPageUrl = SERVER_URL+"/views/index.html";

    layui.use(['jquery', 'table', 'form','layer'], function() {
        var $ = layui.jquery;
        var form = layui.form;
        layer.config({
            anim: 5
            ,offset: '50px'
            ,maxmin: true
            ,isOutAnim: false,
            shade: [0.5, '#eee']
        })

        //嵌套问题
        if (window != top){
            top.location.href = location.href;
        }

        //初始化验证码
        refreshVerifyCode();

        //刷新验证码
        function refreshVerifyCode() {
            $(".layadmin-user-login-codeimg").attr("src", verifyUrl+"?timestamp="+Date.parse( new Date()));
        }

        //刷新验证码
        $(".layadmin-user-login-codeimg").click(function () {
            refreshVerifyCode();
        });

        //键盘回车事件
        $(document).on('keyup', function (e) {
            if(e.keyCode == 13){
                $(".loginBtn").click();
            }
        })

        //登录
        form.on('submit(login)', function(data){
            layer.load(2, {time: 5*1000});
            $.ajax({
                type: 'POST',
                url: loginUrl,
                data: data.field,
                success: function (result) {
                    layer.closeAll();
                    if(result.code == 20000){
                        sessionStorage.setItem("userinfo", JSON.stringify(result.data));
                        layer.msg('登录成功', {icon: 1,time: 1000}, function(){
                            window.location.href = loginPageUrl;
                        })
                    }else if(result.code == 501){
                        refreshVerifyCode();
                        layer.msg(result.msg, {time: 1500});
                    }else {
                        refreshVerifyCode();
                        layer.msg("用户名或密码错误", {time: 1500});
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>